import init from '@feidao/web/framework/browser/d/component';
import { id, spaceid, systemid, refsystemid } from './config';
import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import { send_msg } from './config';
import storage_set from '@feidao/web/atom/browser/storage/put';
import storage_get from '@feidao/web/atom/browser/storage/get';
import nodejs from '@feidao/web/atom/browser/msg/nodejs';

// import hide from '../../atom-web/hide';
// import show from '../../atom-web/show';
import error from '@feidao/web/atom/browser/msgbox/error';
import direct from '@feidao/web/atom/browser/url/direct-to';

import a001 from './a001';




const actions = {
	a001,
	cmpt_show(fd: IFeidaoAiBrowserComponent, ...args: any[]) {

		fd.data.node.querySelector<HTMLDivElement>('[zjc4]').classList.remove('display-n');
		// console.log(fd.data.node.querySelector<HTMLDivElement>('[zjc4]'))
		// console.log('-----', fd.data.node)
		// return show(fd.data.node);
	},
	close_cmpt(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		fd.data.node.querySelector<HTMLDivElement>('[zjc4]').classList.add('display-n');

		// return hide(fd.data.node);
	},
	change_role_no(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		// console.log(args);
		const target_node = args[0].target;
		const role_no = target_node.getAttribute('data-feidao-role_no');
		// console.log(role_no);
		return storage_set('role_no', role_no);
	},
	async role_sure(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		const role_no = await storage_get('role_no');
		if (!role_no) {
			error('请选择角色');
			return;
		}
		const param = { condition: { role_no, clienttype: '3' } };

		const res = await nodejs<{ result: boolean }>(send_msg, 'web/change-role', param, false, false);
		if (res.result) {
			// 切换角色成功
			direct(fd.data.url, false);

		} else {
			error('切换角色失败');
			// 切换角色失败
		}

	}
};

const s = {
	'cmpt_show': 'cmpt_show',
	'close_cmpt': 'close_cmpt',
	'role_sure': 'role_sure',
	'change_role_no': 'change_role_no',
	'eu-001': 'a001'
};

export default function main(url: string, query: any) {

	return init(id, s, actions, url, query, { spaceid, systemid, refsystemid });
}
